package com.springcloud.sc.ucpv5.client.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil {

	public static String getCurData() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String curDate = null;
		try {
			curDate = dateFormat.format(new Date());
		} catch (Exception e) {
		}

		return curDate;
	}

	public static String getCurData_yyyyMMdd() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		String curDate = null;
		try {
			curDate = dateFormat.format(new Date());
		} catch (Exception e) {
		}

		return curDate;
	}

	public static String getCurTime() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
		String curDate = null;
		try {
			curDate = dateFormat.format(new Date());
		} catch (Exception e) {
		}

		return curDate;
	}

	public static long getCurTime_Long() {
		return (new Date()).getTime();
	}

	/**
	 * 取得当前日期时间的字符串类型
	 * 
	 * @return
	 */
	public static String getCurDataTime() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String curDate = null;
		try {
			curDate = dateFormat.format(new Date());
		} catch (Exception e) {
		}

		return curDate;
	}

	/**
	 * 取得当前日期时间的字符串类型
	 * 
	 * @return
	 */
	public static String getCurDataTime_() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String curDate = null;
		try {
			curDate = dateFormat.format(new Date());
		} catch (Exception e) {
		}
		return curDate;
	}

	/**
	 * 从日期转为字符串
	 * 
	 * @param date
	 * @return
	 */

	public static String getStringFromDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			String c = sdf.format(date);
			return c;

		} catch (Exception e) {
			// TODO: handle exception
			// e.printStackTrace();
		}

		return "";

	}

	/**
	 * 从日期转为字符串
	 * 
	 * @param date
	 * @return
	 */

	public static String getStringFromDate(String dateFormat, String date_String) {
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);

		Date date = null;
		try {
			date = sdf.parse(date_String);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		return getStringFromDate(date);
	}

	/**
	 * 取得简单日期显示
	 * 
	 * @param date_String
	 * @return
	 */
	public static String getShortStringFromDate(String date_String) {
		if (date_String == null || "".equals(date_String)) {
			return "";
		}
		try {
			return getStringFromTime("dd HH:mm:ss", getDateFromString(date_String));
		} catch (Exception e) {
			// TODO: handle exception
		}

		return "";
	}

	/**
	 * 取得简单日期显示
	 * 
	 * @param date_String
	 * @return
	 */
	public static String getShortStringFromDate_dd_HHMM(String date_String) {
		if (date_String == null || "".equals(date_String)) {
			return "";
		}
		try {
			return getStringFromTime("dd HH:mm", getDateFromString(date_String));
		} catch (Exception e) {
			// TODO: handle exception
		}

		return "";
	}

	/**
	 * 取得简单日期显示
	 * 
	 * @param date_String
	 * @return
	 */
	public static String getShortStringFromDate_HHMM(String date_String) {
		if (date_String == null || "".equals(date_String)) {
			return "";
		}
		try {
			return getStringFromTime("HH:mm", getDateFromString(date_String));
		} catch (Exception e) {
			// TODO: handle exception
		}

		return "";
	}

	/**
	 * 从日期转为时间字符串
	 * 
	 * @param date
	 * @return
	 */

	public static String getStringFromTime(String dateFormat, Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		try {
			String c = sdf.format(date);
			return c;

		} catch (Exception e) {
			// TODO: handle exception
			// e.printStackTrace();
		}

		return "";
	}

	/**
	 * 从日期转为时间字符串
	 * 
	 * @param date
	 * @return
	 */

	public static String getStringFromTime(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		try {
			String c = sdf.format(date);
			return c;

		} catch (Exception e) {
			// TODO: handle exception
			// e.printStackTrace();
		}

		return "";
	}

	/**
	 * 从日期对象转为日期字符串
	 * 
	 * @param date
	 * @return
	 */

	public static String getDateStringFromTime(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			String c = sdf.format(date);
			return c;

		} catch (Exception e) {
			// TODO: handle exception
			// e.printStackTrace();
		}

		return "";
	}

	/**
	 * 从字符串转为日期
	 * 
	 * @param dateString
	 * @return
	 */
	public static Date getDateFromString(String dateString) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date date = null;
		try {
			date = sdf.parse(dateString.trim());
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		return date;
	}

	/**
	 * 从字符串转为日期
	 * 
	 * @param dateString
	 * @return
	 */
	public static Date getDateFromString_yyyyMMdd(String dateString) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Date date = null;
		try {
			date = sdf.parse(dateString);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		return date;
	}

	/**
	 * 从字符串转为时间
	 * 
	 * @param dateString
	 * @return
	 */
	public static Date getTimeFromString(String timeString) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

		Date date = null;
		try {
			date = sdf.parse(timeString);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		return date;
	}

	private static Date standardDate = new Date(0);// PublicUtil.stringToDate("1970-01-01","yyyy-mm-dd");

	public static String getHanBanDateFromString(String timeString) {
		String[] tmp = timeString.split(" ");
		// 日期
		if (tmp.length != 2) {
			return "";
		}

		Calendar now = Calendar.getInstance();
		int cur_date = now.get(Calendar.DATE);

		int tmp_date = Integer.parseInt(tmp[0]);

		StringBuffer buffer = new StringBuffer();

		int month = now.get(Calendar.MONTH) + 1;

		if (cur_date != tmp_date && cur_date > tmp_date) {
			// 表示是下个月的
			month += 1;
		}

		// "yyyy-MM-dd HH:mm:ss"
		buffer.append(now.get(Calendar.YEAR));
		buffer.append("-");
		if (month < 10) {
			buffer.append("0");
		}
		buffer.append(month);

		buffer.append("-");
		buffer.append(timeString);

		// 增加秒的描述
		buffer.append(":00");

		return buffer.toString();
	}

	public static int getBetweenDays(String t1, String t2) throws ParseException {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		int betweenDays = 0;
		Date d1 = format.parse(t1);
		Date d2 = format.parse(t2);
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(d1);
		c2.setTime(d2);
		// 保证第二个时间一定大于第一个时间
		if (c1.after(c2)) {
			c1 = c2;
			c2.setTime(d1);
		}
		int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
		betweenDays = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
		for (int i = 0; i < betweenYears; i++) {
			c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
			betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);
		}
		return betweenDays;
	}
}
